﻿using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.RoomTypes
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class RoomTypeServices : ApplicationService, IRoomTypeServices
    {

        IRepository<RoomType> _roomTypeRepository;

        public RoomTypeServices(IRepository<RoomType> roomTypeRepository)
        {
            _roomTypeRepository = roomTypeRepository;
        }

        /// <summary>
        /// 添加房型
        /// </summary>
        /// <param name="roomTypeDTO">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost("RoomTypeAdd")]
        public async Task<ApiResult> RoomTypeAdd(RoomTypeDTO roomTypeDTO)
        {
            try
            {
                var list = ObjectMapper.Map<RoomTypeDTO, RoomType>(roomTypeDTO);

                await _roomTypeRepository.InsertAsync(list);
                return ApiResult.Ok(data: list);
            }
            catch (Exception ex)
            {

                ex.HelpLink = "操作失败";
                return ApiResult.Fail(msg: ex.Message);
            }

        }
        /// <summary>
        /// 房型的显示
        /// </summary>
        /// <param name="roomTypeName">房型名称</param>
        /// <returns>返回方式</returns>
        [HttpGet("RoomTypeShow")]
        public async Task<ResultDto<List<RoomTypeDTO>>> RoomTypeRemove(string roomTypeName, Guid id)
        {
            try
            {
                var list = await _roomTypeRepository.GetQueryableAsync();
                if (!string.IsNullOrEmpty(roomTypeName))
                {
                    list = list.Where(x => x.RoomName == roomTypeName);
                }
              
                if (id != Guid.Empty)
                {
                        list = list.Where(x => x.Id == id);
                }
 
              
                return new ResultDto<List<RoomTypeDTO>>()
                {
                    Data = ObjectMapper.Map<List<RoomType>, List<RoomTypeDTO>>(list.ToList())
                };
            }
            catch (Exception ex)
            {
                // Handle exception
                throw;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">数组</param>
        /// <returns>返回任务</returns>
        [HttpDelete("BatchRoomTypeDel")]
        public async Task<ApiResult> BatchRoomTypeDel(List<Guid> ids)
        {
            var list = await _roomTypeRepository.GetListAsync(m=>ids.Contains(m.Id));

            await _roomTypeRepository.DeleteManyAsync(list);
            return ApiResult.Ok(data: list);


            /*try
            {
                foreach (var id in ids)
                {
                    var roomType = await _roomTypeRepository.GetAsync(x => x.Id == Guid.Parse(id));
                    if (roomType != null)
                    {
                        roomType.IsDeleted = true;
                        await _roomTypeRepository.UpdateAsync(roomType);
                    }
                }
                return ApiResult.Ok();
            }
            catch (Exception ex)
            {
                return ApiResult.Fail(msg: ex.Message);
            }*/
        }
        /// <summary>
        /// 房型更新
        /// </summary>
        /// <param name="RoomTypeUpdateDTO">实体</param>
        /// <returns>返回实体</returns>
        [HttpPut("RoomTypeUpdate")]
        public async Task<ApiResult> RoomTypeUpdate(RoomTypeUpdateDTO RoomTypeUpdateDTO)
        {
            try
            {
                var roomType = await _roomTypeRepository.FirstOrDefaultAsync(x=>x.Id== RoomTypeUpdateDTO.Id);
                if (roomType == null)
                {
                    return ApiResult.Fail(msg: "RoomType not found");
                }

                ObjectMapper.Map(RoomTypeUpdateDTO, roomType);
                await _roomTypeRepository.UpdateAsync(roomType);
                return ApiResult.Ok(data: roomType);
            }
            catch (Exception ex)
            {
                return ApiResult.Fail(msg: ex.Message);
            }
        }
        /// <summary>
        /// 房型单删
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回方法</returns>
        [HttpDelete("RoomTypeDel")]
        public async Task<ApiResult> RoomTypeDelete(Guid id)
        {
            try
            {
                var list = await _roomTypeRepository.GetListAsync(x => x.Id == id);
                await _roomTypeRepository.DeleteAsync(list.FirstOrDefault());
                return ApiResult.Ok();
            }
            catch (Exception ex)
            {
                return ApiResult.Fail(msg: ex.Message);
            }
        }
    }
}
